home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Moscow ML 1.42 / src / toolssrc / Mosmldep.sml < prev    next >
Encoding:
Text File  |  1997-08-18  |  2.7 KB  |  98 lines  |  [TEXT/Moml]

  1. (* Mosmldep -- computing dependencies in a Moscow ML source directory.
  2.    Handles strings and nested comments correctly; normalizes file names
  3.    under DOS.
  4.  
  5.    Usage: mosmldep
  6. *)
  7.  
  8. fun manglefilename s = s
  9.  
  10. open BasicIO List
  11.  
  12. (* Lexer of stream *)
  13.  
  14. fun createLexerStream (is : instream) =
  15.   Lexing.createLexer (fn buff => fn n => Nonstdio.buff_input is buff 0 n)
  16. ;
  17.  
  18. fun parsePhraseAndClear parsingFun lexingFun lexbuf =
  19.   let val phr =
  20.     parsingFun lexingFun lexbuf
  21.     handle x => (Parsing.clearParser(); raise x)
  22.   in
  23.     Parsing.clearParser();
  24.     phr
  25.   end;
  26.  
  27. val parseFile =
  28.   parsePhraseAndClear Deppars.MLtext Deplex.Token;
  29.  
  30. fun addExt s ext = s ^ "." ^ ext
  31.  
  32. local 
  33.     fun say s = (output(std_out, s); flush_out std_out)
  34.     val col = ref 0
  35.     and res = ref [];
  36.  
  37.     fun outstring s =
  38.         if !col + size s >= 76 then
  39.             (print ("\\\n    " ^ s ^ " ");
  40.              col := 5 + size s)
  41.         else
  42.             (print (s ^ " ");
  43.              col := !col + size s + 1);
  44. in
  45.     fun outname s =
  46.         if FileSys.access (addExt s "sig", []) then
  47.             res := addExt s "ui" :: !res
  48.         else if FileSys.access (addExt s "sml", []) then
  49.             res := addExt s "uo" :: !res
  50.         else ();
  51.  
  52.     fun beginentry objext target =
  53.         let val targetname = addExt target objext
  54.         in 
  55.             res := [targetname ^ ":"];
  56.             if objext = "uo" andalso FileSys.access(addExt target "sig", [])
  57.             then res := addExt target "ui" :: !res else ()
  58.         end;
  59.  
  60.     fun endentry () =
  61.         if length(!res) > 1 then
  62.             (col := 0; 
  63.              app outstring (rev (!res));
  64.              print "\n")
  65.         else ();         
  66. end;
  67.  
  68. fun read srcext objext filename =
  69.     let val is       = open_in (addExt filename srcext)
  70.         val lexbuf   = createLexerStream is
  71.         val mentions = Hasht.new 37 : (string, unit) Hasht.t
  72.         val names    = parseFile lexbuf 
  73.     in 
  74.         beginentry objext (manglefilename filename);
  75.         app (fn name => Hasht.insert mentions name ()) names;
  76.         Hasht.apply (fn name => fn _ => outname (manglefilename name))
  77.                     mentions;
  78.         close_in is;
  79.         endentry ()
  80.     end
  81.     handle Parsing.ParseError _ => output(std_out, "Parseerror!\n");
  82.  
  83. fun processfile filename =
  84.     let (* val _ = output(std_err, "Processing " ^ filename ^ "\n"); *)
  85.         val {base, ext} = Path.splitBaseExt filename
  86.     in 
  87.         case ext of
  88.             SOME "sig" => read "sig" "ui" base
  89.           | SOME "sml" => read "sml" "uo" base
  90.           | _          => ()
  91.     end
  92.  
  93. fun main () =
  94.     (List.app processfile (Mosml.listDir "."))
  95.     handle OS.SysErr (str, _) => output(std_err, str ^ "\n\n")
  96.  
  97. val _ = main ();
  98.